package com.qf.web.sys_user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.utils.ResultUtil;
import com.qf.utils.ResultVo;
import com.qf.web.sys_role.entity.Role;
import com.qf.web.sys_role.service.RoleService;
import com.qf.web.sys_user.entity.PagePram;
import com.qf.web.sys_user.entity.User;
import com.qf.web.sys_user.service.UserService;
import com.qf.web.sys_user_role.entity.UserRole;
import com.qf.web.sys_user_role.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author xufuhan
 * @Date 2024/9/10 11:35
 */
@RestController
@RequestMapping("/api/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private UserRoleService userRoleService;
    //新增用户
    @PostMapping
    public ResultVo addUser(@RequestBody User user){
        //判断账户是否占用
        QueryWrapper<User> query=new QueryWrapper<>();
        query.lambda().eq(User::getUsername,user.getUsername());
        User one = userService.getOne(query);
        if (one !=null){
            return ResultUtil.error("账户已被注册");
        }
        user.setIsAdmin(0);
        //加密密码
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        userService.addUser(user);
        return ResultUtil.success("新增用户成功");
    }

    //编辑用户
    @PutMapping
    public ResultVo editUser(@RequestBody User user){
/*        //判断账户是否占用
        QueryWrapper<User> query=new QueryWrapper<>();
        query.lambda().eq(User::getUsername,user.getUsername());
        User one = userService.getOne(query);
        if (one !=null && one.getUserId().equals(user.getUserId())){
            return ResultUtil.error("账户已被注册");
        }*/
        //更新处理
        userService.editUser(user);
        return ResultUtil.success("更新用户成功");
    }
    //删除用户
    @DeleteMapping("/{userId}")
    public ResultVo deleteUser(@PathVariable("userId") Long userId){
        boolean remove = userService.removeById(userId);
        if (remove){
            return ResultUtil.success("删除用户成功");
        }
        return ResultUtil.error("删除用户失败");
    }

    //列表查询
    @GetMapping("/list")
    public ResultVo getList(PagePram pram){
        IPage<User> list = userService.list(pram);
        //这里处理密码隐藏,使用stream流
        list.getRecords().stream().forEach(item->{
            item.setPassword(null);
        });
        return ResultUtil.success("查询成功",list);
    }

    //角色列表
    @GetMapping("/roleList")
    public ResultVo getRoleList(){
        List<Role> list = roleService.list();
        return ResultUtil.success("查询成功",list);
    }

    //查询用户角色
    @GetMapping("/getRoleByUserId")
    public ResultVo getRoleByUserId(Long userId){
        QueryWrapper<UserRole> wrapper=new QueryWrapper<>();
        wrapper.lambda().eq(UserRole::getUserId,userId);
        UserRole one = userRoleService.getOne(wrapper);
        return ResultUtil.success("查询成功",one);
    }
}
